package com.lkei.controller;

import com.alibaba.fastjson.JSONObject;
import com.lkei.dto.Result;
import com.lkei.entity.SchoolStudentMessage;
import com.lkei.entity.TeacherStudentNotice;
import com.lkei.entity.TeacherStudentTask;
import com.lkei.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("studentMessage")
public class StudentMessageController {
    @Autowired
    private SchoolStudentMessageService schoolStudentMessageService;
    @Autowired
    private TeacherMessageTaskService teacherMessageTaskService;
    @Autowired
    private TeacherMessageNoticeService teacherMessageNoticeService;
    @Autowired
    private SchoolMessageService schoolMessageService;

    @CrossOrigin
    @RequestMapping("findTask")
    public Result findTask(@RequestBody String data){

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        long StudentID = Long.valueOf(map.get("account").toString()).longValue();
        HashMap<String, Object> hashMap = new HashMap<>();
        List<SchoolStudentMessage> schoolTask = schoolStudentMessageService.findTask(StudentID);
        for (SchoolStudentMessage schoolStudentMessage:
                schoolTask) {
            schoolStudentMessage.setPublisher("校方");
            schoolStudentMessage.setTagtype(false);
        }
        List<TeacherStudentTask> teacherTask = teacherMessageTaskService.findTaskByStudentID(StudentID);
        for (TeacherStudentTask teacherStudentTask:
                teacherTask) {
            teacherStudentTask.setPublisher("教师");
            teacherStudentTask.setTagtype(true);
            teacherStudentTask.setMessageType("任务");
        }
        hashMap.put("schoolTask",schoolTask);
        hashMap.put("teacherTask",teacherTask);

        return Result.SUCCESS.setNewData(hashMap);
    }

    @CrossOrigin
    @RequestMapping("findNotice")
    public Result findNotice(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        long StudentID = Long.valueOf(map.get("account").toString()).longValue();
        HashMap<String, Object> hashMap = new HashMap<>();
        List<SchoolStudentMessage> schoolNotice = schoolStudentMessageService.findNotice(StudentID);
        for (SchoolStudentMessage schoolStudentMessage:
                schoolNotice) {
            schoolStudentMessage.setPublisher("校方");
            schoolStudentMessage.setTagtype(false);
        }
        List<TeacherStudentNotice> teacherNotice = teacherMessageNoticeService.findNoticeByStudentID(StudentID);

        for (TeacherStudentNotice teacherStudentNotice:
                teacherNotice) {
            teacherStudentNotice.setPublisher("教师");
            teacherStudentNotice.setTagtype(true);
            teacherStudentNotice.setMessageType("通知");
        }
        hashMap.put("schoolNotice",schoolNotice);
        hashMap.put("teacherNotice",teacherNotice);
        return Result.SUCCESS.setNewData(hashMap);
    }

    @CrossOrigin
    @RequestMapping("findSchoolMessage")
    public Result findSchoolMessage(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        String messageDetail = schoolMessageService.findSchoolStudentMessageDetails(MessageID);
        return Result.SUCCESS.setNewData(messageDetail);
    }

    @CrossOrigin
    @RequestMapping("findTeacherTask")
    public Result findTeacherTask(@RequestBody String data){

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        List<TeacherStudentTask> taskByMessageID = teacherMessageTaskService.findTaskByMessageID(MessageID);

        return Result.SUCCESS.setNewData(taskByMessageID);
    }

    @CrossOrigin
    @RequestMapping("findTeacherNotice")
    public Result findTeacherNotice(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        int MessageID = Integer.valueOf(map.get("message_id").toString()).intValue();
        String message_detail = teacherMessageNoticeService.findNoticeByMessageID(MessageID);
        return Result.SUCCESS.setNewData(message_detail);
    }
}
